package com.project.service.impl;

import com.project.bean.RoomBean;
import com.project.mapper.IFacilityMapper;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class RoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public void addRoom(RoomBean roomBean) {
        SqlSession session = this.getSession();
        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
        mapper.addRoom(roomBean);
        session.commit();
        session.close();
    }

    @Override
    public boolean delRoom(int id) {
        SqlSession session = this.getSession();
        IRoomMapper rMapper =  session.getMapper(IRoomMapper.class);
        IStudentMapper sMapper = session.getMapper(IStudentMapper.class);
        IFacilityMapper fMapper = session.getMapper(IFacilityMapper.class);
        if (sMapper.countByRoomId(id)!=0){
            return false;
        }
        fMapper.delByRoom(id);
        rMapper.delRoom(id);
        session.commit();
        session.close();
        return true;
    }

    @Override
    public RoomBean findById(int id) {
        SqlSession session = this.getSession();
        IRoomMapper rMapper =  session.getMapper(IRoomMapper.class);
        IStudentMapper sMapper = session.getMapper(IStudentMapper.class);
        RoomBean room = rMapper.findById(id);
        room.setStuList(sMapper.findByRoom(id));
        session.close();
        return room;
    }

    @Override
    public List<RoomBean> findByItem(String site, String canLive, String state, String type) {
        SqlSession session = this.getSession();
        IRoomMapper mapper = session.getMapper(IRoomMapper.class);
        List<RoomBean> list = mapper.findByItem(site,canLive,state,type);
        session.close();
        return list;
    }
}
